package com.mr.base.alpha.dao.impl;

import com.mr.base.alpha.dos.UserDO;
import com.mr.base.alpha.dao.UserDao;
import com.mr.base.alpha.mappers.expand.UserExpandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.executor.BatchResult;
import com.mr.base.core.mybatis.utils.BatchResultUtil;

/**
 * (TUser)Dao实现类
 *
 * @author ray
 * @since 2022-01-20 09:56:08
 */
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private UserExpandMapper userExpandMapper;

    @Override
    public int batchUpdate(List<UserDO> doList) {
        if (CollectionUtils.isEmpty(doList)) {
            return 0;
        }

        try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
            UserExpandMapper mapper = session.getMapper(UserExpandMapper.class);

            return Lists.partition(doList, 3000).stream().mapToInt(o -> {
                mapper.batchUpdate(o);

                final List<BatchResult> batchResults = session.flushStatements();

                session.commit();
                session.clearCache();

                return BatchResultUtil.changeCount(batchResults);
            }).sum();
        }
    }


    @Override
    public int batchInsert(List<UserDO> doList) {
        if (CollectionUtils.isEmpty(doList)) {
            return 0;
        }

        try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH )) {
            UserExpandMapper mapper = session.getMapper(UserExpandMapper.class);

            List<List<UserDO>> data = Lists.partition(doList, 5000);

            AtomicInteger count = new AtomicInteger(0);
            for (List<UserDO> datum : data) {

                datum.stream().forEach(e -> mapper.insert(e));

                session.flushStatements();
                session.commit();
                session.clearCache();

            }

            return count.get();
        }
    }

//    @Override
//    public int batchInsert(List<UserDO> doList) {
//        if (CollectionUtils.isEmpty(doList)) {
//            return 0;
//        }
//
//        try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
//            UserExpandMapper mapper = session.getMapper(UserExpandMapper.class);
//
//            return Lists.partition(doList, 3000).stream().mapToInt(o -> {
//
//                for (UserDO e : o) {
//                    mapper.insert(e);
//                }
//
//                final List<BatchResult> batchResults = session.flushStatements();
//                session.commit();
//
//                return BatchResultUtil.changeCount(batchResults);
//            }).sum();
//        }
//    }

    @Override
    public int batchDelete(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return 0;
        }

        try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
            UserExpandMapper mapper = session.getMapper(UserExpandMapper.class);

            return Lists.partition(ids, 3000).stream().mapToInt(o -> {
                mapper.batchDeleteByPrimaryKeys(o);

                final List<BatchResult> batchResults = session.flushStatements();

                session.commit();
                session.clearCache();

                return BatchResultUtil.changeCount(batchResults);
            }).sum();
        }
    }

}

